#include "mylabel.h"
#include <QDebug>
#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
#include "gamemanager.h"
MyLabel::MyLabel(QWidget *parent) :
    QLabel(parent),
    m_bLeftPress(false)
{
    this->setWindowFlags(Qt::FramelessWindowHint);    //取消窗体的标题栏
    setMouseTracking(true);
    this->setWordWrap(true);
    //this->setFont(GameManager::Instance().font);
    //MouseMoveEvent为了不太耗资源在默认状态下是要鼠标按下才能捕捉到，要想鼠标不按下时的移动也能捕捉到设置为true
    this->resize(100,100);
    this->setParent(parent);
    connect(&EventCenter::Instance(),&EventCenter::SelectUIChanged,this,[=](){
        if(!selected)
            return;
        selected = false;
        repaint();
    });
    connect(&EventCenter::Instance(),&EventCenter::UIChange,this,[=](QString name){
        if(info && name == info->name && UILib::Instance().labels.contains(name))
        {
            this->Refresh(UILib::Instance().labels[name]);
        }
    });
}

MyLabel::~MyLabel()
{
}

bool MyLabel::IsSelected()
{
    return selected;
}

void MyLabel::mouseMoveEvent(QMouseEvent *event)
{
    QLabel::mouseMoveEvent(event);
    if(EventCenter::Instance().GetApplicationMode())
    {
        return;
    }
    if(selectDisabled)
        return;
    //缩放
    //窗体不是最大的话就改变鼠标的形状
    if(Qt::WindowMaximized != windowState())
    {
        setCursorShape(CalCursorPos(event->pos(),CalCursorCol(event->pos())));
    }
    //获取当前的点，这个点是全局的
    QPoint ptCurrentPos = QCursor::pos();
    //计算出移动的位置，当前点 - 鼠标左键按下的点
    QPoint ptMoveSize = ptCurrentPos - m_ptViewMousePos;
    QRect rtTempGeometry = m_rtPreGeometry;
    if(m_bLeftPress)
    {
        switch(m_iCalCursorPos)
        {
        case TOPLEFT:
            rtTempGeometry.setTopLeft(m_rtPreGeometry.topLeft()+ptMoveSize);
            break;
        case TOP:
            rtTempGeometry.setTop(m_rtPreGeometry.top()+ptMoveSize.y());
            break;
        case TOPRIGHT:
            rtTempGeometry.setTopRight(m_rtPreGeometry.topRight()+ptMoveSize);
            break;
        case LEFT:
            rtTempGeometry.setLeft(m_rtPreGeometry.left()+ptMoveSize.x());
            break;
        case RIGHT:
            rtTempGeometry.setRight(m_rtPreGeometry.right()+ptMoveSize.x());
            break;
        case BUTTOMLEFT:
            rtTempGeometry.setBottomLeft(m_rtPreGeometry.bottomLeft()+ptMoveSize);
            break;
        case BUTTOM:
            rtTempGeometry.setBottom(m_rtPreGeometry.bottom()+ptMoveSize.y());
            break;
        case BUTTOMRIGHT:
            rtTempGeometry.setBottomRight(m_rtPreGeometry.bottomRight()+ptMoveSize);
            break;
        default:
            break;
        }
        //移动窗体，如果比最小窗体大，就移动
        if(rtTempGeometry.width() >= 5 && rtTempGeometry.height() >= 5)
            setGeometry(rtTempGeometry);

    }
}

void MyLabel::mousePressEvent(QMouseEvent *event)
{
    QLabel::mousePressEvent(event);
    if(EventCenter::Instance().GetApplicationMode() || selectDisabled)
    {
        return;
    }
    m_iCalCursorPos = CalCursorPos(event->pos(),CalCursorCol(event->pos()));
    if (event->button() == Qt::LeftButton)
    {
        if(m_iCalCursorPos != CENTER)
        {
            m_bLeftPress = true;
        }
        else
        {
            if(selectDisabled)
            {
                selected = false;
                return;
            }
            if(!selected)
            {
                emit EventCenter::Instance().SelectUIChanged();
            }
            selected = !selected;
            repaint();

        }
    }
    m_rtPreGeometry = geometry();
    m_ptViewMousePos = event->globalPos();
}

void MyLabel::mouseReleaseEvent(QMouseEvent *event)
{
    QLabel::mouseReleaseEvent(event);
    if(EventCenter::Instance().GetApplicationMode())
    {
        return;
    }
    m_bLeftPress = false;
    QApplication::restoreOverrideCursor();
}

int MyLabel::CalCursorCol(QPoint pt)
{
    return (pt.x() < FRAMESHAPE ? 1 : ((pt.x() > this->width() - FRAMESHAPE) ? 3 : 2));
}

int MyLabel::CalCursorPos(QPoint pt, int colPos)
{
    return ((pt.y() < FRAMESHAPE ? 10 : ((pt.y() > this->height() - FRAMESHAPE) ? 30 : 20)) + colPos);
}

void MyLabel::setCursorShape(int CalPos)
{
    Qt::CursorShape cursor;
    switch(CalPos)
    {
    case TOPLEFT:
    case BUTTOMRIGHT:
        cursor = Qt::SizeFDiagCursor;
        break;
    case TOPRIGHT:
    case BUTTOMLEFT:
        cursor = Qt::SizeBDiagCursor;
        break;
    case TOP:
    case BUTTOM:
        cursor = Qt::SizeVerCursor;
        break;
    case LEFT:
    case RIGHT:
        cursor = Qt::SizeHorCursor;
        break;
    default:
        cursor = Qt::ArrowCursor;
        break;
    }
    setCursor(cursor);
}

void MyLabel::Refresh(LabelInfo *labelInfo)
{
    if(!labelInfo)
        return;
    if(!labelInfo && !info)
        return;
    if(!labelInfo)
    {
        labelInfo = info;
    }
    else
    {
        info = labelInfo;
    }
    if(!labelInfo->img.isEmpty())
    {
        labelInfo->img.replace("\\","/");
        QString qss = "QLabel{border-image:url(" + labelInfo->img + ");}";
        if(UILib::Instance().labelWhiteList.contains(info->name))
        {
            QString obName = this->objectName();
            qss = "QLabel{border-image:url(" + labelInfo->img + ");padding-left: 40px;padding-right: 40px;";
            //color
            qss += "color:rgb(" + QString::number(labelInfo->color.red()) + "," +
                        QString::number(labelInfo->color.green()) + "," +
                        QString::number(labelInfo->color.blue()) + ");}";
            this->setFont(GameManager::Instance().font);
        }
        this->setStyleSheet(qss);
    }
    if(!UILib::Instance().labelWhiteList.contains(info->name))
        this->setText(labelInfo->text);
}

void MyLabel::SetSelectedDisabled(bool disable)
{
    selectDisabled = disable;
}

void MyLabel::paintEvent(QPaintEvent *e)
{
    QLabel::paintEvent(e);
    if(!selected)
        return;
    QPainter painter(this);
    QPen pen;
    pen.setColor(Qt::red);
    pen.setWidth(3);
    painter.setPen(pen);
    painter.drawRect(this->rect());
    painter.end();
}
